package sy.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.SysAccountDaoI;
import sy.model.SysAccount;
import sy.util.Encrypt;

@Repository("sysAccountDao")
public class SysAccountDaoImpl implements SysAccountDaoI {
	
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}
	
	@Override
	public Serializable save(SysAccount sysAccount) {
		return getCurrentSession().save(sysAccount);//直接用
		
	}

	@Override
	public void remove(SysAccount sysAccount) {
		// TODO Auto-generated method stub
		getCurrentSession().delete(sysAccount);
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysAccountDaoI#update(sy.model.SysAccount)
	 */
	@Override
	public void update(SysAccount sysAccount) {
		// TODO Auto-generated method stub
		getCurrentSession().update(sysAccount);
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysAccountDaoI#list()
	 */
	@Override
	public List<SysAccount> list() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from SysAccount").list();
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysAccountDaoI#getSysAccountById(java.lang.String)
	 */
	@Override
	public SysAccount getSysAccountById(String id) {
		// TODO Auto-generated method stub
		String hql = "from SysAccount where id = :id";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("id", id);
		List<SysAccount> list = query.list();
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysAccountDaoI#listSysAccountByKey(java.lang.String)
	 */
	@Override
	public List<SysAccount> listSysAccountByKey(String key) {
		// TODO Auto-generated method stub
		String hql = "from SysAccount where name like :key";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("key", "%"+key+"%");
		List<SysAccount> list = query.list();
		return list;
	}
	
	/* (non-Javadoc)
	 * @see sy.dao.SysAccountDaoI#listSysAccountByKey(java.lang.String)
	 */
	@Override
	public SysAccount findByNameAndPwd(String name, String password) {
		
		String hql = "from SysAccount where name = :name and password = :password";
		Query query = getCurrentSession().createQuery(hql);
				
		query.setString("name",name);
		query.setString("password", Encrypt.e(password));
		List<SysAccount> list = query.list();
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	
	@Override
	public SysAccount findByName(String telephone) {
		
		String hql = "from SysAccount where name = :name ";
		Query query = getCurrentSession().createQuery(hql);
				
		query.setString("name",telephone);
		List<SysAccount> list = query.list();
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public SysAccount getSysAccountByToken(String token) {
		// TODO Auto-generated method stub
		String hql = "from SysAccount where token = :token ";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("token",token);
		List<SysAccount> list = query.list();
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public void updateLoginTime(String token) {
		// TODO Auto-generated method stub
		String hql = "update SysAccount set loginTime = null where token = :token";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("token",token);
		query.executeUpdate();
	}

	@Override
	public void modifyPassword(String telephone, String password) {
		// TODO Auto-generated method stub
		String hql = "update SysAccount set password = :password where name = :telephone";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("telephone",telephone);
		query.setString("password",Encrypt.e(password));
		query.executeUpdate();
	}

}
